//
//  AuthLoginService.m
//  MobileChecker
//
//  Created by Lymons on 15-1-5.
//  Copyright (c) 2015年 Lymons. All rights reserved.
//

#import "AuthLoginService.h"
#import "NetRequest.h"

@interface AuthLoginService ()

@property (strong, nonatomic) NSMutableSet *requests;

@end

@implementation AuthLoginService

- (instancetype)init {
    self = [super init];
    if (self) {
        //
        _requests = [NSMutableSet new];
    }
    return  self;
}

- (RACSignal *)signalWithURL:(NSString *)url
                   arguments:(NSDictionary *)args
                         transform:(id (^)(NSDictionary *response))block {

    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        [NetRequest NetRequestPOSTWithRequestURL:url WithParameter:args WithReturnValueBlock:^(id returnValue) {
            NSDictionary *response = block(returnValue);
            [subscriber sendNext:response];
            [subscriber sendCompleted];
        } WithFailureBlock:^(NSError *error){
            DDLog(@"Network Error.");
//            NSString *errmsg = [[NSString alloc] initWithData:error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] encoding:NSUTF8StringEncoding];
//            DDLog(@"%@", errmsg);
            [subscriber sendError:error];
        }];
        
        return [RACDisposable disposableWithBlock:^{

        }];
    }];
}

- (RACSignal *)authLoginSignal:(NSString *)codeString
{
    return [self signalWithURL:API_AUTH
                     arguments:@{KEY_AUTHCD : codeString}
                     transform:^id(NSDictionary *response) {
                               
                               return response;
                           }];
}

- (RACSignal *)sendResultSignal:(NSDictionary *)params
{
    return [self signalWithURL:API_SEND
                     arguments:params
                     transform:^id(NSDictionary *response) {
                               
                               return response;
                           }];
}

- (RACSignal *)sendPriceSignal:(NSDictionary *)params
{
    return [self signalWithURL:API_PRICE
                     arguments:params
                     transform:^id(NSDictionary *response) {
                         
                         return response;
                     }];
}

@end
